

/**
 * 中国区域
 * @param view ID
 * @param parentId  上级ID
 * @param defaultId 默认ID
 * @param clearIds  需要清空的ID
 */
function region(view, parentId, defaultId, clearIds) {
    if (typeof(parentId) == 'undefined') {
        parentId = 0;
    }
    if (typeof(defaultId) == 'undefined') {
        defaultId = 0;
    }
    $.ajax({
        type: "GET",
        url: "/admin/api/brands/"+parentId+"?random="+Math.random(),
        dataType: "json",
        async: false,
        success: function(response){
            $("#"+view).html("<option value=''>请选择</option>");
            if (response.code === 0) {
                let data = response.data;
                $.each(data, function(key, val){
                    if (val.id != defaultId) {
                        $("#"+view).append("<option value='"+val.id+"'>"+val.name+"</option>");
                    } else {
                        $("#"+view).append("<option value='"+val.id+"' selected='selected'>"+val.name+"</option>");
                    }
                });
                if (typeof(clearIds) != 'undefined') {
                    clearId(clearIds);
                }
            }
            return true;
        }
    });
}

/**
 * 清空
 * @param ids
 */
function clearId(ids) {
    var view = ids.split(',');
    for (var i = 0; i < view.length; i++) {
        var objId = $.trim(view[i]);
        if ($("#"+objId).length > 0) {
            $("#"+objId).html("<option value=''>请选择</option>");
        }
    }
}

/**
 *
 * @param view
 * @param parentId
 * @param defaultId
 */
function carType(view, parentId, defaultId) {
    if (typeof(parentId) == 'undefined') {
        parentId = 0;
    }
    if (typeof(defaultId) == 'undefined') {
        defaultId = 0;
    }
    $.ajax({
        type: "GET",
        url: "/api/car_type/"+parentId+"?random="+Math.random(),
        dataType: "json",
        async: false,
        success: function(response){
            $("#"+view).html("<option value=''>请选择</option>");
            if (response.code == 0) {
                $.each(response.data, function(key, val){
                    if (val.id != defaultId && response.data.length != 1) {
                        $("#"+view).append("<option value='"+val.id+"'>"+val.name+"</option>");
                    } else {
                        $("#"+view).append("<option value='"+val.id+"' selected='selected'>"+val.name+"</option>");
                    }
                });
            } else {
                alert(response.message);
            }
        }
    });

}

/**
 * 复制链接
 * @param id
 * @param view
 */
function copyUrl(id, view) {
    var Url=document.getElementById(id);
    Url.select(); // 选择对象
    document.execCommand("Copy"); // 执行浏览器复制命令
    if (!view) {
        alert("已复制好，可贴粘。");
    } else {
        $("#"+view).html('已复制好，可贴粘。');
        $("#"+view).show();
        $("#"+view).fadeIn('slow',function() {
            $("#"+view).fadeOut('slow');
        });
    }
}

/**
 *@ justDigit
 *@ INPUT输入字符串限制为 整数
 *@ id : 要控制的input ID
 */
function justDigit (obj) {
    $(obj).val($(obj).val().replace(/[^0-9]/g,''));
}

/**
 *@ justFloat
 *@ INPUT输入字符串限制为 浮点数
 */
function justFloat (obj) {
    $(obj).val($(obj).val().replace(/[^0-9.]/g,''))
}

/**
 *@ justChar
 *@ INPUT输入字符串限制为 英文字符
 */
function justChar (obj) {
    $(obj).val($(obj).val().replace(/[^a-zA-Z]/g,''))
}

/**
 *@ justDigitLine
 *@ INPUT输入字符串限制为 整数-_
 */
function justDigitLine (obj) {
    $(obj).val($(obj).val().replace(/[^0-9\-_]/g,''));
}

/**
 *@ justMoney
 *@ INPUT输入字符串限制为 浮点数
 */
function justMoney (obj) {
    var money = $(obj).val();
    pregMatchN = /^[-]?[1-9][0-9]*([.][\d]{1,2})?$/;
    pregMatchF = /^[-]?[0]([.][\d]{1,2})?$/;
    if (!pregMatchN.test(money) && !pregMatchF.test(money)) {
        $(obj).val('');
    }
}

/**
 * 转为浮点型的值
 * @param obj
 * @returns {*}
 */
function parseFloatVal(obj) {
    if ($('#'+obj).val() == '' || $('#'+obj).val() == '0') {
        return 0;
    } else {
        if ($('#'+obj).length > 0) {
            return parseFloat($('#'+obj).val().replace(/,/g,''));
        } else {
            return 0;
        }
    }
}

/**
 * 传入ID,转为浮点型的值
 * @param obj
 * @returns {*}
 */
function parseIntVal(obj) {
    if ($('#'+obj).val() == '' || $('#'+obj).val() == '0') {
        return 0;
    } else {
        if ($('#'+obj).length > 0) {
            return parseInt($('#'+obj).val().replace(/,/g,''));
        } else {
            return 0;
        }
    }
}


/**
 * 提交表单
 * @param method
 * @param url
 * @param data
 * @param callback
 */
function ajaxForm(method, url, data, callback) {
    if (data != '') {
        data = JSON.stringify(data.serializeJson());
    } else {
        data = null;
    }
    var index = null;
    $.ajax({
        type: method,
        url: url,
        data: data,
        dataType:"json",
        async: false,
        beforeSend: function (request) {
            // 禁用按钮防止重复提交
            //$("#submit").attr({ disabled: "disabled" });
            index = layer.load(2, {shade:false}); //0代表加载的风格，支持0-2 time:3000,
            request.setRequestHeader("Content-type", "application/json");
        },
        success: function(response) {
            if (response.code > '0') {
                layer.msg(response.message);
            } else {
                if (method != 'get') {
                    layer.msg('Successful Operation!',{time:1000}, function () {
                        if (!callback) {
                            if (response.jump != '') {
                                document.location.href = response.jump;
                            } else {
                                window.location.reload();
                            }
                        } else {
                            callback(response);
                        }
                    });
                } else {
                    if (callback) {
                        callback(response);
                    }
                }
            }
            layer.close(index);
        },
        complete: function () {
            //$("#submit").removeAttr("disabled");
            layer.close(index);
        },
        error: function (data) {
            console.info("error: " + data.responseText);
        }
    });
}

/**
 * post 提交表单
 * @param method
 * @param url
 * @param data
 * @param callback
 */
function ajaxGo(method, url, data, callback) {
    var index = null;
    $.ajax({
        type: method,
        url: url,
        data: data,
        dataType:"json",
        async: false,
        beforeSend: function (request) {
            // 禁用按钮防止重复提交
            //$("#submit").attr({ disabled: "disabled" });
            index = layer.load(2, {shade:false}); //0代表加载的风格，支持0-2 time:3000,
            if (method == 'PUT' || method == 'put') {
                request.setRequestHeader("Content-type", "application/json");
            }
        },
        success: function(response) {
            if (response.code > '0') {
                layer.msg(response.message);
            } else {
                if (method != 'get') {
                    layer.msg('Successful Operation!',{time:1000}, function () {
                        if (!callback) {
                            if (response.jump != '') {
                                document.location.href = response.jump;
                            } else {
                                window.location.reload();
                            }
                        } else {
                            callback(response);
                        }
                    });
                } else {
                    if (callback) {
                        callback(response);
                    }
                }
            }
            layer.close(index);
        },
        complete: function () {
            //$("#submit").removeAttr("disabled");
            layer.close(index);
        },
        error: function (data) {
            console.info("error: " + data.responseText);
        }
    });
}

/**
 * 导出
 * @param url
 */
function ajaxExport(url) {
    //获取表单对象
    var param = $("#searchForm").serialize();
    if (url.indexOf('?') == -1) {
        url += '?'+param;
    } else {
        url += '&'+param;
    }
    window.open(url);
}

/**
 * 将对象转为json
 */
(function($){
    $.fn.serializeJson=function(){
        var serializeObj = {};
        var array = this.serializeArray();
        //var str = this.serialize();
        $(array).each(function(){
            if(serializeObj[this.name]){
                if($.isArray(serializeObj[this.name])){
                    serializeObj[this.name].push(this.value);
                }else{
                    serializeObj[this.name]=[serializeObj[this.name],this.value];
                }
            }else{
                serializeObj[this.name]=this.value;
            }
        });
        return serializeObj;
    };
})(jQuery);

function resetForm(form,element) {

    layer.msg('请稍后...', {
        time: 500 //
    });

    if (element) {
        element.tabChange('tabForm', 1);
    }

    $("#postForm")[0].reset();

    $(':input','#postForm').val("").not(':button,:submit,:reset,:hidden').removeAttr('checked').removeAttr('checked');

    form.render();

}
